using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JobSeekers_register : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString =
                            WebConfigurationManager.ConnectionStrings[
                            "Database2000"].ConnectionString;
        if(Session["jsuserid"]==null)
        {
            lblHeader.Text = "New Registration";
            this.Title = "::New Registration";
            literalMessage.Visible = true;
            trTermofService.Visible=true;
            trAgree.Visible = true;
            txtEmail.Enabled=true;
            btnSave.Text = "I Agree";
            
            Page.ClientScript.RegisterClientScriptBlock(
                    this.GetType(), "ClientScriptFunctionAgree",
                    ClientScriptFunctionAgree(), true);       
            btnSave.Attributes.Add("onclick","return validateAgree();");
            trPassword.Visible = true;
            trRetype.Visible = true;
            btnNotAgree.Visible=true;
        }
        else
        {
            this.Title = "::Update Login Information";
            literalMessage.Visible=false;
            trTermofService.Visible = false;
            trAgree.Visible=false;
            lblHeader.Text = "Update Login Information";
            txtEmail.Enabled = false;
            txtEmail.Text= Session["jsemail"].ToString();
            btnSave.Text="Update";
            trPassword.Visible = false;
            trRetype.Visible = false;
            btnNotAgree.Visible=false;
            if(!IsPostBack)
                EditAccount();
        }
    }
    void EditAccount()
    {
        SqlServerProvider cls = new SqlServerProvider(connectionString);
        cls.CommandText = "select FirstName,"
        +"LastName,Newsletter,JobAlert,"+
        "CompanyInfo from tblJSAccounts" +
        " where UserID='" + Session["jsuserid"]+
         "'";
        DataTable dataTable=
        cls.GetDataTable( );
        txtFirstName.Text= dataTable.Rows[0][0].ToString();
        txtLastName.Text = dataTable.Rows[0][1].ToString();
        chkNewLetter.Checked = (dataTable.Rows[0][2].ToString() == "True");
        chkJobAlert.Checked = (dataTable.Rows[0][3].ToString() == "True");
        chkCompanyInfo.Checked = (dataTable.Rows[0][4].ToString() == "True");
    }
    string ClientScriptFunctionAgree()
    {
        string strScript = "";
        strScript =
        "function validateAgree()\n" +
        "{\n if(!document.getElementById('" 
        + chkAgree.ID + "').checked){alert('"
        +"Please check Agree checkbox "+
        "if you agree our policy');"+
        "return false;} return true;" +        
        " \n}\n";
        return strScript;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        
                    
        if(txtEmail.Text!="")
        {
            SqlServerProvider cls = new SqlServerProvider(connectionString);
            if (Session["jsuserid"] == null)
            {                                
                string userID=cls.NewJSAccount(
                txtEmail.Text, 
                txtPassword.Text,
                txtFirstName.Text, 
                txtLastName.Text,
                (chkNewLetter.Checked)?"1":"0",
                (chkJobAlert.Checked)?"1":"0",
                (chkCompanyInfo.Checked)?"1":"0");
                if(cls.ErrorCode == 2627)
                {
                    literalError.Text=
                    "Sorry! RCV database show that "+
                    "someone has registered a job "+
                    "seeker account with this e-mail "+
                    "address. If you have previously "+
                    "registered under this e-mail "+
                    "address and forgotten your "+
                    "password, you can request for "+
                    "your password <a href=\""+
                    "forgotpassword.aspx\">here</a>";
                }
                else if (cls.ErrorMessage  !="")
                {
                    literalError.Text =
                    "Error: " + cls.ErrorMessage;
                }
                else               
                {
                    MailClass mailClass = new MailClass();
                    mailClass.SendMail(txtEmail.Text, 
                    "New Registration","Thanks you for" +
                    " your registration at RecruitVietnam.com.");
                    Session.Add("jsuserid", userID);
                    Session.Add("jsemail", txtEmail.Text);
                    Session.Add("jsfullname", 
                    txtFirstName.Text + " " + txtLastName.Text);
                    literalError.Text = "";                 
                }
            }
            else
            {
                literalError.Text = "Updated successfull";
                cls.UpdateJSAccount(
                Session["jsuserid"].ToString() ,
                txtEmail.Text,               
                txtFirstName.Text,
                txtLastName.Text,
                (chkNewLetter.Checked) ? "1" : "0",
                (chkJobAlert.Checked) ? "1" : "0",
                (chkCompanyInfo.Checked) ? "1" : "0");
                Session["jsfullname"]= txtFirstName.Text 
                + " " + txtLastName.Text;
            }
            Response.Redirect("personal.aspx");
        }
        else  
        {
            literalError.Text =
            "Please check on Agree before submit";
        }
    }
}
